package com.health.banner.controller;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.banner.domain.ContentBanner;
import com.health.banner.service.IContentBannerService;

/**
 * 用户端广告Controller
 */
@RestController
@RequestMapping("/user/banner")
public class UserBannerController extends BaseController {

    @Autowired
    private IContentBannerService contentBannerService;

    /**
     * 用户端查询广告列表 - 不需要权限
     * 只查询启用状态且在有效期内的广告
     */
    @GetMapping("/list")
    public AjaxResult list() {
        ContentBanner contentBanner = new ContentBanner();
        // 只查询启用状态的广告
        contentBanner.setStatus("0");
        List<ContentBanner> list = contentBannerService.selectContentBannerList(contentBanner);
        
        // 过滤当前时间在有效期内的广告
        Date now = new Date();
        List<ContentBanner> validBanners = list.stream()
            .filter(banner -> 
                (banner.getStartTime() == null || !banner.getStartTime().after(now)) &&
                (banner.getEndTime() == null || !banner.getEndTime().before(now))
            )
            .toList();
            
        return success(validBanners);
    }
}